#include <iostream>
#include <vector>
#include <iterator>

#include <boost/shared_ptr.hpp>

typedef std::vector<std::size_t> IntVec;
typedef boost::shared_ptr<IntVec> IntVecPtr;
typedef std::vector<IntVecPtr> Int2Vec;

class Solver
{
public:
  Solver(std::size_t limit_)
    : count(0), limit(limit_)
  { }

  bool
  hasAnswerLeft()
  {
    if (count < limit)
      {
	++count;

	IntVecPtr V1(new IntVec);

	for (std::size_t i = 0; i < count; ++i)
	  {
	    V1->push_back(i);
	  }

	V = V1;

	return true;
      }

    return false;
  }

  IntVecPtr
  getNextAnswer()
  {
    return V;
  }


private:
  std::size_t count;
  std::size_t limit;
  IntVecPtr V;
};


int 
main()
{
  Solver s(5);
  IntVecPtr V;

  Int2Vec Vs;

  while (s.hasAnswerLeft())
    {
      V = s.getNextAnswer();
      std::copy(V->begin(), V->end(), std::ostream_iterator<std::size_t>(std::cout, " "));
      std::cout << std::endl;

      Vs.push_back(V);
    }

  std::cout << "Reprinting..." << std::endl;

  for (Int2Vec::const_iterator it = Vs.begin(); it != Vs.end(); ++it)
    {
      IntVecPtr tmp = *it;
      std::copy(tmp->begin(), tmp->end(), std::ostream_iterator<std::size_t>(std::cout, " "));
      std::cout << std::endl;
    }
}
